/*****************************************************************************
 * $LastChangedDate: 2010-03-01 14:19:14 -0500 (Mon, 01 Mar 2010) $
 * @file
 * @author  Jim E. Brooks  http://www.palomino3d.org
 * @brief   OSG utility functions.
 *//*
 * LEGAL:   COPYRIGHT (C) 2009 JIM E. BROOKS
 *          THIS SOURCE CODE IS RELEASED UNDER THE TERMS
 *          OF THE GNU GENERAL PUBLIC LICENSE VERSION 2 (GPL 2).
 *****************************************************************************/

#define GFX_OSG_FUNCS_CC 1
#include "base/module.hh"
#include "base/file.hh"
using namespace base;
#include "gfx/module.hh"
#include "gfx/osg.hh"
#include "gfx/osg_funcs.hh"

#include <osgDB/WriteFile>

namespace gfx {

////////////////////////////////////////////////////////////////////////////////
//////////////////////////////  OSG node functions  ////////////////////////////
////////////////////////////////////////////////////////////////////////////////

/*****************************************************************************
 * Remove all child nodes.
 *****************************************************************************/
void
OsgNodeRemoveChildren( RefPtr<osg::Group> group )
{
    const uint n = group->getNumChildren();
    group->removeChildren( 0, n );
}

/*****************************************************************************
 * Dump a node (sub-graph) to a file.
 *****************************************************************************/
bool
OsgNodeSaveToFile( osg::Node& node, const string& filename )
{
ASSERT( not filename.empty() );

    // OSG minor bug: osgDB::writeNodeFile() has a few cases where
    // it will fail to write but incorrectly returns true.
    bool wrote = osgDB::writeNodeFile( node, filename );
    if ( not IfFileExists( filename ) )
        wrote = false;  // really failed
    return wrote;
}

////////////////////////////////////////////////////////////////////////////////
//////////////////////////  OSG geometry functions  ////////////////////////////
////////////////////////////////////////////////////////////////////////////////

/*****************************************************************************
 * Call this after modifying elements vertexs (while keeping the same size).
 *****************************************************************************/
void
OsgGeometryWasModified( RefPtr<osg::Geometry> geom,
                        RefPtr<osg::Vec3Array> vertexs )
{
    // Update affected components of osg::Geometry.
    geom->setVertexArray( vertexs.get() );      // recomputes bounding box, calls dirtyDisplayList()
}

/*****************************************************************************
 * Call this after adding or removing vertexs.
 * NOTE: Assumes osg::Geometry has one PrimitiveSet.
 *****************************************************************************/
void
OsgGeometryWasModifiedByResizing( RefPtr<osg::Geometry> geom,
                                  RefPtr<osg::Vec3Array> vertexs,
                                  RefPtr<osg::DrawArrays> drawArray )
{
    // Update affected components of osg::Geometry.
    geom->setVertexArray( vertexs.get() );      // recomputes bounding box, calls dirtyDisplayList()
    drawArray->setCount( vertexs->size() );     // updates glDrawArray() parameters (OpenGL vertex array)
}

} // namespace gfx
